package com.x.erp.controller;

import java.util.Date;
import java.util.List;


import com.x.erp.model.CarLog;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.Car;
import com.x.erp.model.CarDel;
import com.x.erp.rpc.CarDelRpcService;
import com.x.erp.rpc.CarLocationRpcService;
import com.x.erp.rpc.CarRpcService;
import com.x.erp.rpc.ModelRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

import javax.servlet.http.HttpServletRequest;

/**
 * 已删除车辆信息
 * @author pansong
 *
 */
@RestController
@RequestMapping(value = "/carDel")
public class CarDelController extends BaseController {

	@Reference
	private CarDelRpcService carDelRpcService;//已删除车辆信息服务
	
	@Reference
	private CarRpcService carRpcService;//车辆信息服务
	
	@Reference
	private ModelRpcService modelRpcService;//车型服务
	
	@Reference
	private CarLocationRpcService carLocationRpcService;//车牌所在地信息服务

	/**
	 * 新增已删除车辆信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertCarDel", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertCar(Car car, HttpServletRequest request) {
		String resource = request.getHeader("resource");
		if(car == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆信息不能为空！");
		}
		if(StringUtils.isEmpty(car.getCarId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID不能为空！");
		}
		if(StringUtils.isEmpty(car.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "操作用户ID不能为空！");
		}
		//不允许逻辑删除的情况
		
		car = carRpcService.selectCar(new Car(car.getCarId()));
		if(car == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID不正确！");
		}
		CarDel carDel = new CarDel(car);
		carDel.setDelUserId(car.getUserId());
		// 记录日志
		CarLog carLog = new CarLog("新增车辆逻辑删除信息", new Date(), carDel.getDelUserId(), carDel.getCarId());
		carLog.setCarLogSource(resource);
		carDelRpcService.insertCarDel(carDel, carLog);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功", carDel);
	}

	/**
	 * 查询已删除车辆信息列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getCarDelList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCarDelList(CarDel carDel) {
		List<CarDel> selectCarDelList = carDelRpcService.selectCarDelList(carDel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功",selectCarDelList );
	}
	
	/**
	 * 根据ID查询已删除车辆信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getCarDelById", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCarDelById(String carId) {
		CarDel carDel = new CarDel();
		if(StringUtils.isEmpty(carId)) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID不能为空！");
		}
		carDel.setCarId(carId);
		carDel = carDelRpcService.selectCarDel(carDel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carDel);
	}

}
